require("../../utils/util.js");

var a = getApp();

Page({
    data: {
        userId: wx.getStorageSync("userId")
    },
    onLoad: function(t) {
        this.setData({
            id: t.id,
            t: t.type
        });
        var e = a.globalData.interRechargrDetail;
        this.getBaseData(e);
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    getBaseData: function(a) {
        var t = this;
        wx.request({
            url: a,
            data: {
                type: t.data.t,
                id: t.data.id
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var e = JSON.parse(a.data);
                0 != e.code ? wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 2e3
                }) : t.setData({
                    card: e.data
                });
            }
        });
    },
    buy: function(a) {
        var t = this;
        t.setData({
            type: a.currentTarget.dataset.type,
            balance: a.currentTarget.dataset.balance
        }), t.addOrder(t.data.type);
    },
    addOrder: function(t) {
        var e = this, o = a.globalData.baseUrl + a.globalData.urlAddCardOrder, n = wx.getStorageSync("phone");
        n = n || a.globalData.phone, e.setData({
            orderPhone: n
        }), n && n.length > 0 ? wx.request({
            url: o,
            data: {
                phone: n,
                cardId: e.data.id,
                type: t,
                userId: a.globalData.userId,
                pay: e.data.balance,
                storeId: a.globalData.storeId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var t = JSON.parse(a.data);
                0 != t.code ? (wx.showToast({
                    title: t.msg,
                    icon: "none",
                    duration: 2e3
                }), console.log(t.msg)) : e.getPayData(t.msg);
            }
        }) : wx.showToast({
            title: "请先绑定手机号",
            icon: "none",
            duration: 1e3
        });
    },
    getPayData: function(t) {
        var e = this, o = a.globalData.urlBase + a.globalData.urlPayData;
        wx.request({
            url: o,
            data: {
                orderId: t,
                userId: a.globalData.userId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var t = JSON.parse(a.data);
                0 != t.code ? wx.showToast({
                    title: t.msg,
                    icon: "none",
                    duration: 2e3
                }) : e.wxPay(t);
            }
        });
    },
    wxPay: function(t) {
        var e = this;
        wx.requestPayment({
            timeStamp: t.timeStamp,
            nonceStr: t.nonceStr,
            package: "prepay_id=" + t.totalId,
            signType: "MD5",
            paySign: t.sign,
            success: function(t) {
                e.setData({
                    page: 1,
                    hasMore: !0
                }), wx.navigateTo({
                    url: "../huiyuan/myhuiyuan?storeId=" + a.globalData.ekStoreId + "&phone=" + e.data.orderPhone + "&type=" + e.data.type
                });
            },
            fail: function(a) {
                wx.showToast({
                    title: "支付失败",
                    icon: "none",
                    duration: 800
                });
            },
            complete: function(a) {}
        });
    },
    onShareAppMessage: function() {}
});